/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.sadhar.sipp.perekamankodeglunitkerja;

import java.util.HashMap;
import java.util.Map;
import org.sadhar.sia.framework.ClassApplicationModule;
import org.zkoss.zk.ui.Component;
import org.zkoss.zul.Button;
import org.zkoss.zul.Label;
import org.zkoss.zul.Messagebox;
import org.zkoss.zul.Textbox;
import org.zkoss.zul.Tree;
import org.zkoss.zul.Treechildren;
import org.zkoss.zul.Treeitem;

/**
 *
 * @author hendro
 */
public class PerekamanKodeGLUnitKerjaWnd extends ClassApplicationModule {

    private Tree treeUnKerjaTop;
    private Tree treeUnKerjaBottom;
    private Label lblNamaIndukUnitKerja;
    private Label lblNamaUnitKerja;
    private Textbox txtboxKodeGLUnit;
    private Textbox txtboxIdUnitKerja;
    private Button btnSimpan;
    private Button btnBatal;
    private PerekamanKodeGLUnitKerjaDAO unitKerjaDAO;

    public PerekamanKodeGLUnitKerjaWnd() {
        this.unitKerjaDAO = new PerekamanKodeGLUnitKerjaDAOImpl();
    }

    public void onCreate() throws Exception {
        treeUnKerjaTop = (Tree) getFellow("treeUnKerjaTop");
        treeUnKerjaBottom = (Tree) getFellow("treeUnKerjaBottom");
        lblNamaIndukUnitKerja = (Label) getFellow("lblNamaIndukUnitKerja");
        lblNamaUnitKerja = (Label) getFellow("lblNamaUnitKerja");
        txtboxKodeGLUnit = (Textbox) getFellow("txtboxKodeGLUnit");
        btnSimpan = (Button) getFellow("btnSimpan");
        btnBatal = (Button) getFellow("btnBatal");
        txtboxIdUnitKerja = (Textbox) getFellow("txtboxIdUnitKerja");
        loadTreeUnKerjaTop();
        loadTreeUnKerjaBotton();
    }

    public void loadTreeUnKerjaTop() throws Exception {
        treeUnKerjaTop.removeChild(treeUnKerjaTop.getFirstChild());
        treeUnKerjaTop.clear();
        Treechildren child = new Treechildren();
        Map map = new HashMap();
        map.put(0, child);
        int tmpIdInduk = 0;
        for (PerekamanKodeGLUnitKerja unit : unitKerjaDAO.list()) {
            Treeitem item = new Treeitem();
            item.setId(String.valueOf(unit.getIdUnitKerja()));
            item.setValue(unit);
            item.setLabel(unit.getNama_unit_kerja());
            Component comp = (Component) map.get(unit.getIdIndukUnitKerja());
            if (tmpIdInduk == unit.getIdIndukUnitKerja()) {
                item.setParent(comp);
                map.put(unit.getIdUnitKerja(), item);
            } else {
                Treechildren grandChild = new Treechildren();
                grandChild.appendChild(item);
                grandChild.setParent(comp);
                map.put(unit.getIdUnitKerja(), item);
                map.put(unit.getIdIndukUnitKerja(), grandChild);
                tmpIdInduk = unit.getIdIndukUnitKerja();
            }
        }
        treeUnKerjaTop.appendChild((Component) map.get(0));
    }

    public void loadTreeUnKerjaBotton() throws Exception {
        treeUnKerjaBottom.removeChild(treeUnKerjaBottom.getFirstChild());
        Treechildren child = new Treechildren();
        Map map = new HashMap();
        map.put(0, child);
        int tmpIdInduk = 0;
        for (PerekamanKodeGLUnitKerja unit : unitKerjaDAO.list()) {
            Treeitem item = new Treeitem();
            item.setId("bottom" + String.valueOf(unit.getIdUnitKerja()));
            item.setValue(unit);
            if (unit.getKodeGLUnit().length() > 0) {
                item.setLabel(unit.getNama_unit_kerja() + " - " + unit.getKodeGLUnit());
            } else {
                item.setLabel(unit.getNama_unit_kerja());
            }
            Component comp = (Component) map.get(unit.getIdIndukUnitKerja());
            if (tmpIdInduk == unit.getIdIndukUnitKerja()) {
                item.setParent(comp);
                map.put(unit.getIdUnitKerja(), item);
            } else {
                Treechildren grandChild = new Treechildren();
                grandChild.appendChild(item);
                grandChild.setParent(comp);
                map.put(unit.getIdUnitKerja(), item);
                map.put(unit.getIdIndukUnitKerja(), grandChild);
                tmpIdInduk = unit.getIdIndukUnitKerja();
            }
        }
        treeUnKerjaBottom.appendChild((Component) map.get(0));
    }

    public void treeUnKerjaBottomSelect() throws Exception {
        PerekamanKodeGLUnitKerja unit = (PerekamanKodeGLUnitKerja) treeUnKerjaBottom.getSelectedItem().getValue();
        if (unit.getIdIndukUnitKerja() > 0) {
            lblNamaIndukUnitKerja.setValue(unitKerjaDAO.select(unit.getIdIndukUnitKerja()).getNama_unit_kerja());
        } else {
            lblNamaIndukUnitKerja.setValue("......................");
        }
        lblNamaUnitKerja.setValue(unit.getNama_unit_kerja());
        txtboxKodeGLUnit.setValue(unit.getKodeGLUnit());
        txtboxIdUnitKerja.setValue(String.valueOf(unit.getIdUnitKerja()));
    }

    public void treeUnKerjaTopOnSelect() throws Exception {
        PerekamanKodeGLUnitKerja unit = (PerekamanKodeGLUnitKerja) treeUnKerjaTop.getSelectedItem().getValue();
        if (unit.getIdIndukUnitKerja() > 0) {
            lblNamaIndukUnitKerja.setValue(unitKerjaDAO.select(unit.getIdIndukUnitKerja()).getNama_unit_kerja());
        } else {
            lblNamaIndukUnitKerja.setValue("......................");
        }
        lblNamaUnitKerja.setValue(unit.getNama_unit_kerja());
        txtboxKodeGLUnit.setValue(unit.getKodeGLUnit());
        txtboxIdUnitKerja.setValue(String.valueOf(unit.getIdUnitKerja()));
    }

    public void btnSimpanOnClick() throws Exception {
        if (txtboxIdUnitKerja.getValue().isEmpty()) {
            Messagebox.show("Silahakan Pilih Unit Kerja", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
            return;
        }
        if (txtboxKodeGLUnit.getValue().isEmpty()) {
            Messagebox.show("Kode GL harus diisi", "Pesan Informasi", Messagebox.OK, Messagebox.INFORMATION);
            return;
        }
        PerekamanKodeGLUnitKerja unit = unitKerjaDAO.select(Integer.valueOf(txtboxIdUnitKerja.getValue()));
        unit.setKodeGLUnit(txtboxKodeGLUnit.getValue());
        try {
            unitKerjaDAO.update(unit);
            loadTreeUnKerjaTop();
            loadTreeUnKerjaBotton();
        } catch (Exception ex) {
            Messagebox.show("Telah terjadi Error: " + ex.getMessage(), "Pesan Kesalahan", Messagebox.OK, Messagebox.ERROR);
            return;
        }
        clear();
    }

    public void btnBatalOnClick() throws Exception {
        clear();
    }

    public void clear() throws Exception {
        lblNamaIndukUnitKerja.setValue("......................");
        lblNamaUnitKerja.setValue("......................");
        txtboxIdUnitKerja.setValue("");
        txtboxKodeGLUnit.setValue("");
        treeUnKerjaTop.clearSelection();
    }
}
